//
//  UBeatTimer.h
//  UltraStarDeluxeIOS
//
//  Created by 李 杰军 on 12-7-14.
//  Copyright (c) 2012年 DoReiMi. All rights reserved.
//

#ifndef UltraStarDeluxeIOS_UBeatTimer_h
#define UltraStarDeluxeIOS_UBeatTimer_h

#include "UTime.h"

/**
 * TLyricsState contains all information concerning the
 * state of the lyrics, e.g. the current beat or duration of the lyrics.
 */
class TLyricsState
{
private:
    TRelativeTimer* fTimer; // keeps track of the current time
    TSyncSource* fSyncSource;
    float fAvgSyncDiff;
    float fLastClock; // last master clock value
    // Note: do not use Timer.GetState() to check if lyrics are paused as
    // Timer.Pause() is used for synching.
    bool fPaused;
    
    float Synchronize(float LyricTime);
    
public:
    int OldBeat; // previous discovered beat
    int CurrentBeat; // current beat (rounded)
    float MidBeat; // current beat (float)
    
    // now we use this for super synchronization!
    // only used when analyzing voice
    // TODO: change ...D to ...Detect(ed)
    int OldBeatD; // previous discovered beat
    int CurrentBeatD; // current discovered beat (rounded)
    float MidBeatD; // current discovered beat (float)
    
    // we use the for audible clicks
    // TODO: Change ...C to ...Click
    int OldBeatC; // previous discovered beat
    int CurrentBeatC; 
    float MidBeatC; // like CurrentBeatC
    
    int OldLine; // previous displayed sentence
    
    float StartTime; // time till start of lyrics (= Gap)
    float TotalTime; // total song time
    
public:
    TLyricsState();
    
    virtual ~TLyricsState();
    
    /**
     * Resets the LyricsState state.
     */
    void Reset();
    
    void UpdateBeats();
    
    /**
     * Sets a master clock for this LyricsState. If no sync-source is set
     * or SyncSource is nil the internal timer is used.
     */
    void SetSyncSource(TSyncSource* SyncSource);
    
    /**
     * Starts the timer. This is either done
     * - immediately if WaitForTrigger is false or
     * - after the first call to GetCurrentTime() / SetCurrentTime() or Start(false)
     */
    void Start(bool WaitForTrigger = false);
    
    /**
     * Pauses the timer.
     * The counter is preserved and can be resumed by a call to Start().
     */
    void Pause();
    
    /**
     * Stops the timer.
     * The counter is reset to 0.
     */
    void Stop();
    
    /**
     * Returns/Sets the current song time (in seconds) used as base-timer for lyrics etc.
     * If GetCurrentTime()/SetCurrentTime() if Start() was called
     */
    float GetCurrentTime();
    void SetCurrentTime(float Time);
};

#endif
